# !!! 存疑

def method_1(database_name, database_username, database_password, database_host, database_port):
    # 安全性低，效率高

    # A connection can be used in more than a with statement and each with block is effectively wrapped in a separate transaction:

    # conn = psycopg2.connect(DSN)
    # with conn:
    #     with conn.cursor() as curs:
    #         curs.execute(SQL1)
    # with conn:
    #     with conn.cursor() as curs:
    #         curs.execute(SQL2)
    # conn.close()
    import pymysql

    connection = pymysql.connect(db=database_name, user=database_username, password=database_password, host=database_host, port=int(database_port), charset='utf8')
    print(connection.open)
    connection.close()

    with pymysql.connect(db=database_name, user=database_username, password=database_password, host=database_host, port=int(database_port), charset='utf8')as connection:
        with connection.cursor() as cursor:
            pass

def method_2(database_name, database_username, database_password, database_host, database_port):
    # 安全，模块化，需要在程序中维护数据库结构

    import sqlalchemy
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy import func

    database_service = 'mysql+pymysql'
    database_name += '?charset=utf8'
    url = f'{database_service}://{database_username}:{database_password}@{database_host}:{database_port}/{database_name}'
    engine = sqlalchemy.create_engine(url)
    db_session = sessionmaker(bind=engine)
    session = db_session()
    # query_result = session.query(<table>).filter(condition)